#include "fileItemWidget.h"
#include <QDesktopServices>
#include <QUrl>
#include <QFileDialog>
#include <QDebug>
#include <QScreen>
#include <QWindow>
#include <QApplication>
#include <QFileInfo>


/**
 * 文件展示组件
 * @brief FileItemWidget::FileItemWidget
 * @param item
 * @param parent
 */

FileItemWidget::FileItemWidget(const ClipboardItem& item, QWidget *parent)
    : QWidget(parent), item(item),
      fileLabel(new QLabel(this)),
      titleLabel(new QLabel("文件", this)),
      fileIconLabel(new QLabel(this)),
      copyButton(new QPushButton("", this)),
      deleteButton(new QPushButton("", this)),
      openButton(new QPushButton("", this)),
      favoriteButton(new QPushButton("",this)),
      mainLayout(new QVBoxLayout(this)),
      favoriteTextLayout(new QHBoxLayout()),
      buttonLayout(new QHBoxLayout())
{

    //设置按钮样式
    copyButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/copy_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/copy_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/copy_button_pressed.png);"
                "}"
                );
    deleteButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/delete_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/delete_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/delete_button_pressed.png);"
                "}"
                );
    openButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/open_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/open_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/open_button_pressed.png);"
                "}"
                );

    // 设置标题标签的字体和大小
    titleLabel->setStyleSheet("QLabel {font-size: 18px; font-family: 'Dengxian'; font-weight: bold;}"); // 设置标题为粗体
    titleLabel->setContentsMargins(5,5,5,0);

    mainLayout->addWidget(titleLabel);

    // 获取主屏幕的分辨率
       QScreen *screen = QGuiApplication::primaryScreen();
       QRect screenGeometry = screen->geometry();
       int screenWidth = screenGeometry.width();
       int screenHeight = screenGeometry.height();
       fileLabel->setFixedWidth(screenWidth*0.13);
       fileLabel->setMaximumWidth(screenWidth*0.13);
       fileLabel->setMaximumHeight(screenHeight*0.10);

       // 获取文件名
       QString filePath = item.getDisplayText();
       QFileInfo fileInfo(filePath);
       QString fileName = fileInfo.fileName();

       // 设置文件名到标签上
       fileLabel->setText(fileName);

    if (!item.getDisplayText().isEmpty() && QFileInfo(item.getDisplayText()).exists()) {
        //获取icon
        QFileIconProvider provider;
        QFileInfo info = QFileInfo(item.getDisplayText());
        QIcon icon = provider.icon(info);
        fileIconLabel->setPixmap(icon.pixmap(30)); // 设置图标大小为48x48

        // 创建一个 QHBoxLayout 用于放置文件图标和文件标签
        QHBoxLayout *iconLayout = new QHBoxLayout();
        iconLayout->addWidget(fileIconLabel); // 添加文件图标标签
        iconLayout->addWidget(fileLabel);   // 添加文件标签

        // 将对齐方式从居中改为左对齐
        iconLayout->setAlignment(Qt::AlignLeft);
        // 添加一些左边距，使其看起来更美观
        iconLayout->setContentsMargins(5, 0, 0, 0);

        // 将图标布局添加到主布局中
        mainLayout->addLayout(iconLayout);

    } else {
        // 处理无效路径的情况，例如设置一个默认图标或显示错误信息
        mainLayout->addWidget(fileLabel);  // 添加文件标签
    }
    // 设置布局
    buttonLayout->addWidget(copyButton);
    buttonLayout->addWidget(openButton);
    buttonLayout->addWidget(deleteButton);

    //button下外边距
    buttonLayout->setContentsMargins(0,0,0,5);

    mainLayout->addLayout(buttonLayout); // 添加按钮布局

    setLayout(mainLayout);

    // 连接信号和槽
    connect(copyButton, &QPushButton::clicked, this, &FileItemWidget::onCopyClicked);
    connect(deleteButton, &QPushButton::clicked, this, &FileItemWidget::onDeleteClicked);
    connect(openButton, &QPushButton::clicked, this, &FileItemWidget::onOpenClicked);
    connect(favoriteButton, &QPushButton::clicked, this, &FileItemWidget::onFavoriteClicked);

    // 添加收藏按钮的样式设置
    QString favoriteImageUrl = item.isFavorite() ? 
        ":/icon/icon/favorite_button_pressed.png" : 
        ":/icon/icon/favorite_button.png";
        
    favoriteButton->setStyleSheet(
        "QPushButton { "
        "image: url(" + favoriteImageUrl + ");"
        "width: 15px;"
        "height: 15px;"
        "min-width: 15px;"
        "min-height: 15px;"
        "max-width: 15px;"
        "max-height: 15px;"
        "background-color: transparent;"
        "border: none;"
        "}"
    );

    // 修改布局设置
    favoriteTextLayout->addWidget(titleLabel);
    favoriteTextLayout->addStretch();
    favoriteTextLayout->addWidget(favoriteButton);

    mainLayout->removeWidget(titleLabel);  // 移除旧的标题布局
    mainLayout->insertLayout(0, favoriteTextLayout);  // 在最上方插入新的布局
}

ClipboardItem FileItemWidget::getItem() const {
    return item;  // 返回存储的 ClipboardItem
}

void FileItemWidget::onCopyClicked() {
    emit copyClicked();  // 发射复制文件路径的信号
}

void FileItemWidget::onDeleteClicked() {
    emit deleteClicked();  // 发射删除文件引用的信号
}

void FileItemWidget::onOpenClicked() {
    emit openClicked();  // 发射打开文件或文件夹的信号
}

void FileItemWidget::onFavoriteClicked() {
    emit favoriteClicked();
    item.setFavorite(!item.isFavorite());  // 切换收藏状态
    updateFavoriteButtonStyle();  // 立即更新按钮样式
}

void FileItemWidget::updateFavoriteButtonStyle() {
    QString favoriteImageUrl = item.isFavorite() ? 
        ":/icon/icon/favorite_button_pressed.png" : 
        ":/icon/icon/favorite_button.png";
        
    favoriteButton->setStyleSheet(
        "QPushButton { "
        "image: url(" + favoriteImageUrl + ");"
        "width: 15px;"
        "height: 15px;"
        "min-width: 15px;"
        "min-height: 15px;"
        "max-width: 15px;"
        "max-height: 15px;"
        "background-color: transparent;"
        "border: none;"
        "}"
    );
}
